﻿Imports DTO
Imports BUS
Public Class ucHangHoa

    Dim hh As New HangHoaDTO

    Private Sub barbtnThemHH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnThemHH.ItemClick
        Dim frm As New frmThemHangHoa
        frm.ShowDialog()
    End Sub

    Sub LayDSHangHoa()
        GCKhuVuc.DataSource = HangHoaBUS.LayDSHangHoa()
    End Sub

    Private Sub ucHangHoa_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        barbtnSuaChuaHH.Enabled = False
        barbtnXoaHH.Enabled = False
        barbtnXuatHH.Enabled = False
        LayDSHangHoa()
    End Sub

    Private Sub barbtnSuaChuaHH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnSuaChuaHH.ItemClick
        Dim frm As New frmThemHangHoa
        frm.kt = 1
        frm.mahh = hh.MaHangHoa
        frm.tenhh = hh.TenHangHoa
        frm.gm = hh.GiaMua
        frm.gbs = hh.GiaBanSi
        frm.gbl = hh.GiaBanLe
        frm.toithieu = hh.TonKhoToiThieu
        frm.matt = hh.MaTrangThai
        frm.anh = hh.DiaChiAnh
        frm.Text = "Sửa chữa hàng hóa, dịch vụ"
        frm.ShowDialog()
            
    End Sub

    Private Sub barbtnXoaHH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnXoaHH.ItemClick
        Dim pq As PhanQuyenNhonNDDTO = ChucNangBUS.layQuyenCuaND(frmMain.maND, 24)
        If pq.Xoa = True Then
            Try
                If HangHoaBUS.XoaHangHoa(hh) Then
                    DevExpress.XtraEditors.XtraMessageBox.Show("Xóa thành công hàng hóa", "Thông tin", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    LayDSHangHoa()
                    HeThongBUS.themNhatKy(frmMain.maND, "Quản lý hàng hóa", "Xóa", hh.MaHangHoa)
                End If
            Catch ex As Exception
                DevExpress.XtraEditors.XtraMessageBox.Show("Hàng hóa này đang sử dụng không thể xóa", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
                TyGiaBUS.DongKetNoi.Close()
            End Try
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Xin lỗi bạn không có quyền này", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If

        
    End Sub

    Private Sub barbtnNapLaiHH_ItemClick(sender As Object, e As DevExpress.XtraBars.ItemClickEventArgs) Handles barbtnNapLaiHH.ItemClick
        LayDSHangHoa()
    End Sub

    Private Sub GridView1_RowClick(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowClickEventArgs) Handles GridView1.RowClick
        If e.RowHandle >= 0 Then
            barbtnSuaChuaHH.Enabled = True
            barbtnXoaHH.Enabled = True
            hh.MaHangHoa = GridView1.GetRowCellValue(e.RowHandle, GCMaHH).ToString()
            hh.TenHangHoa = GridView1.GetRowCellValue(e.RowHandle, GCTenHH).ToString()
            hh.GiaMua = Convert.ToDouble(GridView1.GetRowCellValue(e.RowHandle, GCGiaMuaHH))
            hh.GiaBanSi = Convert.ToDouble(GridView1.GetRowCellValue(e.RowHandle, GCGiaBanSiHH))
            hh.GiaBanLe = Convert.ToDouble(GridView1.GetRowCellValue(e.RowHandle, GCGiaBanLeHH))
            hh.MaTrangThai = Convert.ToInt32(GridView1.GetRowCellValue(e.RowHandle, GCConQuanLyHH))
            hh.TonKhoToiThieu = Convert.ToDouble(GridView1.GetRowCellValue(e.RowHandle, GCToiThieuHH))
            hh.DiaChiAnh = DirectCast(GridView1.GetRowCellValue(e.RowHandle, GCHinh), Byte())
            'MessageBox.Show(GridView1.GetRowCellValue(e.RowHandle, GCHinh).ToString())
        End If
    End Sub

    Private Sub GridView1_CustomDrawRowIndicator(sender As Object, e As DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs) Handles GridView1.CustomDrawRowIndicator
        If e.RowHandle >= 0 Then
            If e.Info.IsRowIndicator Then
                e.Info.DisplayText = (e.RowHandle + 1).ToString()
            End If
        End If
    End Sub
End Class
